package org.kgc.alanhealth.service.impl;
import org.kgc.alanhealth.service.SessionTokenService;
import org.springframework.stereotype.Service;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.TimeUnit;

@Service
public class SessionTokenServiceImpl implements SessionTokenService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public String insertSessionToken(String account) {
        String sessionToken = generateSessionToken();
        redisTemplate.opsForValue().set(sessionToken, account, 6, TimeUnit.HOURS); // 设置过期时间为6小时
        return sessionToken;
    }

    @Override
    public boolean isTokenValid(String token) {
        Object account = redisTemplate.opsForValue().get(token);
        if (account != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean hasAccess(String token) {
        return redisTemplate.opsForValue().get(token) != null;
    }

    @Override
    public boolean deleteByPrimaryKey(String token) {
        redisTemplate.delete(token);
        return true;
    }

    private String generateSessionToken() {
        return UUID.randomUUID().toString();
    }

}